package com.gosophia.conferenceManagement.test.unit;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.hibernate.EntityMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.persister.entity.EntityPersister;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.gosophia.conferenceManagement.common.entity.ConferenceJobData;
import com.gosophia.conferenceManagement.common.entity.Document;
import com.gosophia.conferenceManagement.scheme.entity.SchemeJobData;
import com.gosophia.conferenceManagement.summary.entity.SendSummaryJobData;
import com.gosophia.conferenceManagement.summary.entity.SummaryJobData;
import com.gosophia.dataDictionary.entity.DataEntry;
import com.gosophia.dataDictionary.entity.DataValue;
import com.gosophia.taskManagement.entity.Job;
import com.gosophia.taskManagement.entity.Mission;
import com.gosophia.userManagement.entity.UserInfo;

@ContextConfiguration(locations = { "classpath:/applicationContext-test.xml" })
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class UTSchemeMapping extends
    AbstractTransactionalJUnit4SpringContextTests{

    @Autowired
    private SessionFactory sessionFactory;
    
    @Test
    @SuppressWarnings("unchecked")
    public void allClassMapping() throws Exception {
        Session session = sessionFactory.getCurrentSession();
        Map metadata = sessionFactory.getAllClassMetadata();
        for (Object o : metadata.values()) {
            EntityPersister persister = (EntityPersister) o;
            Object e = generateEntity(persister.getMappedClass(EntityMode.POJO));
            if(e==null) {
                continue;
            }
            session.save(e);
            session.flush();
            Serializable id = persister.getIdentifier(e, EntityMode.POJO);
            session.clear();
            session.get(persister.getMappedClass(EntityMode.POJO), id);
        }
    }

    @SuppressWarnings({ "unchecked" })
    private Object generateEntity(Class entityClass) {
       if(entityClass.equals(SchemeJobData.class)) {
            Session session = sessionFactory.getCurrentSession();
            DataEntry e2 = new DataEntry();
            e2.setEntryName(UUID.randomUUID().toString().substring(0, 5));
            e2.setEntryNameCN(UUID.randomUUID().toString().substring(0, 5));
            e2.setEntryDescliption(UUID.randomUUID().toString().substring(0, 5));
            e2.setEntryType("内置");
            session.save(e2);
            session.flush();

            DataValue o = new DataValue();
            o.setOrderIndex(0);
            o.setIsEnabled(true);
            o.setValue(UUID.randomUUID().toString().substring(0, 5));
            o.setKey("001");

            List<DataValue> dataValues = new ArrayList<DataValue>();
            dataValues.add(o);
            e2.setDataValues(dataValues);
            session.save(o);
            session.flush();
            
            UserInfo e = new UserInfo();
            e.setUserName("马小黑");
            e.setEmail("yahh.com");
            session.save(e);
            session.flush();
            Document e1 = new Document();
            e1.setDocumentCategory("online");
            e1.setDocumentName("我爱中国");
            e1.setDocumentNote("文档备注");
            e1.setDocumentPath("c://");
            e1.setDocumentVersion("1.1");
            session.save(e1);
            session.flush();
            Document e4 = new Document();
            e4.setDocumentCategory("online");
            e4.setDocumentName("我爱中国12313");
            e4.setDocumentNote("文档备注1111");
            e4.setDocumentPath("c://");
            e4.setDocumentVersion("1.1");
            session.save(e4);
            session.flush();
            Mission mission = new Mission();
            mission.setMissionCode("wdzb20110225004");
            mission.setMissionName("伟测试文档001");
            mission.setStartDate(new Date());
            mission.setScheduleEndDate(new Date());
            mission.setDescription("文档任务");
            mission.setMissionState("办理中");
            mission.setProcessInstenceId(15614L);
            mission.setUndertaker(e);
            mission.setCreator(e);
            mission.setUndertaker(e);
            mission.setMissionType(o);
            mission.setImportance(o);
            mission.setMissionDetailURL("getDocMissionDetailAction.action");
            session.save(mission);
            session.flush();
       
            Job job = new Job();
            job.setHandlerURL("http//");
            job.setDetailURL("http//1");
            job.setStartDate(new Date());
            job.setScheduleEndDate(new Date());
            job.setJobType("sumultee");
            job.setJobState("PROCESSED");
            job.setTokenName("/");
            job.setMission(mission);
            job.setUndertaker(e);
            job.setSource(e);
            session.save(job);
            session.flush();
            
            SchemeJobData schemeJobData = new SchemeJobData();
            schemeJobData.setJob(job);
            schemeJobData.setNextApproval(e);
            schemeJobData.setOnlineDocument(e1);
            schemeJobData.setOpinion("处理意见");
            schemeJobData.setTurningAction("PROCESSED");
            schemeJobData.setConferenceCreator(e);
            schemeJobData.setConferenceDescription("描述");
            schemeJobData.setConferenceName("会议名称");
            schemeJobData.setImportantLevel(o);
            schemeJobData.setPlanEndTime(new Date());
            
            return schemeJobData;
        } else{
            return null;
        }
    }
}
